/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.view;

import java.util.List;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.gandulf.guilib.util.Debug;
import com.munchlevel.MunchLevelApplication;
import com.munchlevel.R;
import com.munchlevel.data.PlayerInfo;
import com.munchlevel.util.ThemeManager;
import com.munchlevel.util.ThemeManager.ThemeItem;
import com.munchlevel.view.CounterView.OnViewChangedListener;
import com.munchlevel.view.listener.OnPlayerChangedListener;

public class BasePlayerLayout extends PercentageLayout implements OnViewChangedListener<CounterView> {

	protected CounterView lvlCounter, gearCounter;

	protected PlayerInfo playerinfo;

	protected int maximumLevel = 10;

	private int orientation;

	protected OnPlayerChangedListener onPlayerChangedListener;

	private ThemeItem currentBackgroundItem;

	public BasePlayerLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public BasePlayerLayout(Context context) {
		this(context, null);
	}

	public BasePlayerLayout(Context context, AttributeSet attrs, int defStyle) {
		this(context, attrs, defStyle, null);

	}

	public BasePlayerLayout(Context context, AttributeSet attrs, int defStyle, PlayerInfo playerInfo) {
		super(context, attrs, defStyle);

		if (!isInEditMode()) {
			if (playerInfo != null) {
				this.playerinfo = playerInfo;
			} else
				this.playerinfo = new PlayerInfo();
		}
	}

	@Override
	protected void onFinishInflate() {
		try {
			setFocusable(true);
			setFocusableInTouchMode(false);

			lvlCounter = (CounterView) findViewById(R.id.lvl_counter);
			lvlCounter.setMinimumValue(1);

			gearCounter = (CounterView) findViewById(R.id.gear_counter);
			gearCounter.setMaximumValue(MunchLevelApplication.MAXIMUM_GEAR);
			gearCounter.setMinimumValue(MunchLevelApplication.MINIMUM_GEAR);

			gearCounter.setOnChangedListener(this);
			lvlCounter.setOnChangedListener(this);
		} catch (Exception e) {
			Debug.error(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View#onAttachedToWindow()
	 */
	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();

		setPlayerInfo(playerinfo);
	}

	public PlayerInfo getPlayerInfo() {
		return playerinfo;
	}

	public void setPlayerInfo(PlayerInfo playerinfo) {
		this.playerinfo = playerinfo;
		if (playerinfo != null) {
			setPlayerName(playerinfo.getName());
			setPlayerRace(playerinfo.getRace());
			setPlayerClass(playerinfo.getClazz());
			setLevel(playerinfo.getLevel());
			setGear(playerinfo.getGear());
			setMale(playerinfo.isMale());
			setProfilePicture(playerinfo.getProfile());
		}
	}

	protected void setPlayerName(String name) {
		if (TextUtils.isEmpty(name))
			name = null;

		if (!TextUtils.equals(name, playerinfo.getName())) {
			playerinfo.setName(name);
		}

	}

	protected void setProfilePicture(Bitmap bitmap) {
		playerinfo.setProfile(bitmap);
	}

	protected void setPlayerRace(List<String> race) {
		playerinfo.addRace(race);
	}

	protected void setPlayerClass(List<String> clazz) {
		playerinfo.addClazz(clazz);
	}

	protected void setMale(boolean male) {
		playerinfo.setMale(male);
		updateBackground();
	}

	public boolean updateBackground() {

		Drawable bg_player;
		ThemeItem newThemeItem = null;
		if (getOrientation() != Configuration.ORIENTATION_LANDSCAPE) {
			if (getPlayerInfo().isMale())
				newThemeItem = ThemeItem.BgMale;
			else
				newThemeItem = ThemeItem.BgFemale;
		} else {
			if (getPlayerInfo().isMale())
				newThemeItem = ThemeItem.BgMaleLandscape;
			else
				newThemeItem = ThemeItem.BgFemaleLandscape;
		}

		if (currentBackgroundItem != newThemeItem) {
			bg_player = ThemeManager.getThemeDrawable(newThemeItem);
			setBackgroundDrawable(bg_player);
			currentBackgroundItem = newThemeItem;
		}

		return getBackground() != null;
	}

	public void setMaximumLevel(int lvl) {
		lvlCounter.setMaximumValue(lvl);
	}

	public int getMaximumLevel() {
		return lvlCounter.getMaximumValue();
	}

	public void reset() {
		setLevel(1);
		setGear(0);
	}

	protected void setLevel(int level) {
		lvlCounter.setValue(level);
		playerinfo.setLevel(lvlCounter.getValue());
	}

	protected void setGear(int gear) {
		gearCounter.setValue(gear);
		playerinfo.setGear(gearCounter.getValue());
	}

	public int getTotal() {
		return gearCounter.getValue() + lvlCounter.getValue();
	}

	public void setGearCounterVisibility(boolean visible) {
		int visibility = visible ? View.VISIBLE : View.GONE;
		gearCounter.setVisibility(visibility);
	}

	@Override
	public void onChanged(CounterView counterView, int oldVal, int newVal) {

		if (counterView == gearCounter) {
			setGear(newVal);
			if (onPlayerChangedListener != null) {
				onPlayerChangedListener.onGearChanged(this, oldVal, newVal);
			}
		} else if (counterView == lvlCounter) {
			setLevel(newVal);
			if (onPlayerChangedListener != null) {
				onPlayerChangedListener.onLevelChanged(this, oldVal, newVal);
			}
		}
	}

	public OnPlayerChangedListener getOnPlayerChangedListener() {
		return onPlayerChangedListener;
	}

	public void setOnPlayerChangedListener(OnPlayerChangedListener onPlayerChangedListener) {
		this.onPlayerChangedListener = onPlayerChangedListener;
	}

	public int getOrientation() {
		return orientation;
	}

	public void setOrientation(int orientation) {
		this.orientation = orientation;
		updateBackground();
	}

}
